package com.miyuan.wm.controller;

import com.alibaba.fastjson.JSON;
import com.miyuan.wm.request.AddCustomerRequestVo;
import com.miyuan.wm.request.QueryCustomersRequestVo;
import com.miyuan.wm.request.UpdateCustomerRequestVo;
import com.miyuan.wm.response.*;
import com.miyuan.wm.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CustomerController
 * @Description 客户管理控制器
 */
@RestController
@Api(description = "客户管理控制器", tags = {"客户管理"})
@RequestMapping(value = "/customer")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "获取客户列表接口")
    @PostMapping("/queryCustomers")
    public PageResponse<List<QueryCustomersResponseVo>> listCustomers(@Valid @RequestBody QueryCustomersRequestVo requestVo) {
        log.info("获取客户列表接口请求参数：{}", JSON.toJSONString(requestVo));
        return customerService.listCustomers(requestVo);
    }

    @ApiOperation(value = "新增客户信息接口")
    @PostMapping("/addCustomer")
    public BaseResponse<Boolean> addCustomer(@Valid @RequestBody AddCustomerRequestVo requestVo) {
        log.info("新增客户信息接口请求参数：{}", JSON.toJSONString(requestVo));
        return customerService.addCustomer(requestVo);
    }

    @ApiOperation(value = "修改客户信息接口")
    @PostMapping("/updateCustomer")
    public BaseResponse<Boolean> updateCustomer(@Valid @RequestBody UpdateCustomerRequestVo requestVo) {
        log.info("修改客户信息接口请求参数：{}", JSON.toJSONString(requestVo));
        return customerService.updateCustomer(requestVo);
    }

    @ApiOperation(value = "删除客户信息接口")
    @PostMapping("/deleteCustomers")
    public BaseResponse<Boolean> deleteCustomers(@RequestBody List<Integer> ids) {
        log.info("删除客户信息接口请求参数：{}", JSON.toJSONString(ids));
        return customerService.deleteCustomers(ids);
    }

    @ApiOperation(value = "查询客户信息，供下拉框使用")
    @GetMapping("/queryCustomerforSelect")
    public BaseResponse<List<QueryCustomersResponseVo>> queryCustomerforSelect() {
        return customerService.queryCustomerforSelect();
    }

    @ApiOperation(value = "根据Id查询客户信息")
    @GetMapping("/queryCustomerById")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "客户数据库Id")})
    public BaseResponse<QueryCustomerByIdResponseVo> queryCustomerById(@RequestParam int id) {
        return customerService.queryCustomerById(id);
    }

}
